﻿
using System;
using System.Drawing;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LIK.WakeOnLan.Forms
{
    public partial class FormPopup : XtraForm
    {
        private Size expectedSize = new Size(220, 20);

        private static Form parentForm;

        public FormPopup()
        {
            InitializeComponent();
            this.FormClosing += (s, arg) =>
            {
                parentForm.Focus();
            };
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.Size = this.expectedSize;
        }

        private void ShowPopupInternal(Control popupControl, Control underControl)
        {
            Point screenPoint = underControl.PointToScreen(new Point(0, underControl.Height));
            this.expectedSize = underControl.Size;
            this.Controls.Add(popupControl);
            popupControl.Dock = DockStyle.Fill;
            this.Show();
            this.Location = screenPoint;
            this.FormClosing += (s, e) =>
            {
                this.Controls.Remove(popupControl);
            };
        }

        private void FormPopup_Deactivate(object sender, System.EventArgs e)
        {
            this.Close();
        }

        internal static void SetParent(Form parent)
        {
            parentForm = parent;
        }

        public static void ShowPopup(Control popupControl, Control underControl)
        {
            FormPopup formPopup = new FormPopup();
            formPopup.ShowPopupInternal(popupControl, underControl);
        }




    }
}
